/****************************************************************************
**
** Copyright (C) 2013 Ernesto Barragan <ernestus at gmail dot com>
**
** GNU General Public License Usage
** This file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/

#include "serialportsettings.h"

SerialPortSettings::SerialPortSettings()
{
    QSettings mySettings ;//uses the application general settings

    device = mySettings.value("SerialPort/device").toString() ;
    baudRate = mySettings.value("SerialPort/baudRate", QSerialPort::Baud9600 ).toInt() ;
    dataBits = QSerialPort::DataBits(mySettings.value("SerialPort/dataBits", QSerialPort::Data8 ).toInt()) ;
    parity = QSerialPort::Parity(mySettings.value("SerialPort/parity", QSerialPort::NoParity ).toInt());
    stopBits = QSerialPort::StopBits(mySettings.value("SerialPort/stopBits", QSerialPort::OneStop ).toInt()) ;
    flowControl = QSerialPort::FlowControl(mySettings.value("SerialPort/flowControl", QSerialPort::NoFlowControl ).toInt()) ;
    localEchoEnabled = mySettings.value("SerialPort/localEchoEnabled", false).toBool() ;
}

void SerialPortSettings::save() const
{
    QSettings mySettings ;
    mySettings.setValue("SerialPort/device", device ) ;
    mySettings.setValue("SerialPort/baudRate", baudRate ) ;
    mySettings.setValue("SerialPort/dataBits", dataBits ) ;
    mySettings.setValue("SerialPort/parity", parity ) ;
    mySettings.setValue("SerialPort/stopBits", stopBits ) ;
    mySettings.setValue("SerialPort/flowControl", flowControl ) ;
    mySettings.setValue("SerialPort/localEchoEnabled", localEchoEnabled ) ;
}

bool SerialPortSettings::applyToPort(QSerialPort &port) const
{
    Q_ASSERT( port.isOpen() ) ;
    bool success = true ;
    success = success && port.setBaudRate( baudRate ) ;
    success = success && port.setDataBits( dataBits ) ;
    success = success && port.setParity( parity ) ;
    success = success && port.setStopBits( stopBits ) ;
    success = success && port.setFlowControl( flowControl ) ;
    return success ;
}


const QMap<QSerialPort::Parity, QString>& SerialPortSettings::parityToString()
{
    if( 0 == m_parityStrings.count() )
    {
        /* only initialize the map if really necessary */
        m_parityStrings[ QSerialPort::NoParity ]   = tr("None") ;
        m_parityStrings[ QSerialPort::EvenParity ] = tr("Even") ;
        m_parityStrings[ QSerialPort::OddParity ]  = tr("Odd") ;
        m_parityStrings[ QSerialPort::MarkParity ] = tr("Mark") ;
        m_parityStrings[ QSerialPort::SpaceParity ] = tr("Space") ;
    }

    return m_parityStrings ;
}

const QMap<QSerialPort::StopBits, QString>& SerialPortSettings::stopBitsToString()
{
    if( 0 == m_stopBitsStrings.count() )
    {
        m_stopBitsStrings[ QSerialPort::OneStop ] = tr("1") ;
#ifdef Q_OS_WIN
        m_stopBitsStrings[ QSerialPort::OneAndHalfStop ] = tr("1.5") ;
#endif
        m_stopBitsStrings[ QSerialPort::TwoStop ] = tr("2") ;
    }

    return m_stopBitsStrings ;
}

const QMap <QSerialPort::FlowControl, QString>& SerialPortSettings::flowControlToString()
{
    if( 0 == m_flowControlStrings.count() )
    {
        m_flowControlStrings[ QSerialPort::NoFlowControl ] = tr("None") ;
        m_flowControlStrings[ QSerialPort::HardwareControl ] = tr("RTS/CTS") ;
        m_flowControlStrings[ QSerialPort::SoftwareControl ] = tr("XON/XOFF") ;
    }

    return m_flowControlStrings ;
}

SerialPortSettings::~SerialPortSettings()
{
    save() ;
}
